package lab1;

class SimThread extends Thread 
{
	public SimThread(String str)
	{
		super(str);
	}
                                 
	public void run()
	{
		for (int i = 0; i < 10; i++)
		{
			System.out.println(i + " " + getName());
			try {
				sleep((int)(Math.random() * 1000));
			}
			catch (InterruptedException e)
			{}
		}
		System.out.println("DONE! " + getName());
	}
	
}

class IOThread extends Thread{
	Thread one;
	Thread two;
	
	IOThread(Thread one,Thread two){
		this.one = one;
		this.two = two;
	}
	
	public void run()
	{
		while(true){
			System.out.println(one.getName()+" "+one.getState());
			System.out.println(two.getName()+" "+two.getState());
			try {
				sleep((int)(Math.random() * 1000));
			}catch (InterruptedException e)
			{}
		}
	}
}

public class TwoThreadsGetState 
{
	public static void main (String[] args){
		Thread one = new SimThread("Go to Shanghai??");
		Thread two = new SimThread("Stay here!!");
		IOThread thread = new IOThread(one,two);
		
		System.out.println(one.getName()+" "+one.getState());
		System.out.println(two.getName()+" "+two.getState());
		
		one.start();
		two.start();
		thread.start();
		
		try {
			one.join();
			two.join();
			thread.stop();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		System.out.println(one.getName()+" "+one.getState());
		System.out.println(two.getName()+" "+two.getState());
	}
}

